package leetcode.L152;

class Solution1 {
    public int maxProduct(int[] nums) {
        int len = nums.length;
        int[] maxArr = new int[len];
        int[] minArr = new int[len];
        for (int i = 0; i < len; i++) {
            maxArr[i] = nums[i];
            minArr[i] = nums[i];
        }
        for (int i = 1; i < len; i++) {
            maxArr[i] = Math.max(maxArr[i - 1] * nums[i], Math.max(nums[i], minArr[i - 1] * nums[i]));
            minArr[i] = Math.min(maxArr[i - 1] * nums[i], Math.min(nums[i], minArr[i - 1] * nums[i]));
        }
        int ans = maxArr[0];
        for (int i = 0; i < len; i++) {
            ans = Math.max(ans, maxArr[i]);
        }
        return ans;
    }
}